package com.itlxd.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlxd.dto.PageDTO;
import com.itlxd.entity.Category;
import com.itlxd.exception.ArgsException;
import com.itlxd.global.ErrorCode;
import com.itlxd.global.SysConstants;
import com.itlxd.global.SysMessages;
import com.itlxd.result.ResultVO;
import com.itlxd.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Api(tags = {"分类相关接口"})
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    //分类查询分页列表
    @ApiOperation(value = "分页查询分类列表接口")
    @GetMapping
    public ResultVO<IPage<Category>> queryCategoryByPage(PageDTO pageDTO) {
        IPage<Category> categoryIPage = categoryService.listPageCategories(pageDTO);
        return ResultVO.success(categoryIPage,SysMessages.QUERY_SUCCESS);
    }

    //添加分类
    @ApiOperation(value = "添加分类接口")
    @PostMapping
    public ResultVO<Integer> addCategory(@ApiParam(value = "分类信息", required = true)
                                             @RequestBody Category category) {
        if (null == category || StringUtils.isEmpty(category.getName())) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        Integer effectNum = categoryService.saveCategory(category);
        if (null != effectNum) {
            return ResultVO.success(effectNum, SysMessages.INSERT_SUCCESS);
        }

        return ResultVO.error(SysMessages.INSERT_FAIL);
    }

    //修改分类信息
    @ApiOperation(value = "修改分类信息接口")
    @PutMapping
    public ResultVO<Integer> updateCategory(@ApiParam(value = "分类信息", required = true)@RequestBody
                                                        Category category) {
        if (null == category || category.getId() == null) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        //更新分类信息
        Integer effectNum = categoryService.updateCategory(category);
        if (null == effectNum) {
            return ResultVO.error(SysMessages.UPDATE_FAIL);
        }

        return ResultVO.success(SysMessages.UPDATE_SUCCESS);
    }

    //根据id删除分类
    @ApiOperation(value = "根据id删除分类接口")
    @DeleteMapping("/{id}")
    public ResultVO<Integer> deleteCategoryById(@ApiParam(value = "分类id", required = true)
                                                    @PathVariable("id") Long id) {
        if (null == id) {
            throw new ArgsException(SysMessages.CATEGORY_ID_IS_NULL);
        }

        Integer effectNum = categoryService.removeById(id);
        if (null == effectNum) {
            return ResultVO.error(ErrorCode.DELETE_DEFAULT_ERROR, SysMessages.DELETE_FAIL);
        }

        return ResultVO.success(effectNum, SysMessages.DELETE_SUCCESS);
    }

    @ApiOperation(value = "查询分类列表接口")
    @GetMapping("/list")
    public ResultVO<List<Category>> queryCategoriesByType(@ApiParam(value = "分类信息")
                                                                      Category category) {
        List<Category> categories = categoryService.listCategories(category);
        if (null == categories) {
            return ResultVO.error(ErrorCode.QUERY_DEFAULT_ERROR, SysMessages.QUERY_FAIL);
        }

        return ResultVO.success(categories, SysMessages.QUERY_SUCCESS);
    }
}

